#!/bin/sh

filter_list="none
noremakes
trusted"
category_list="all
anime
anime_mv
anime_english
anime_other
anime_raw
audio
lossless
lossy
literature
literature_english
literature_other
literature_raw
live_action
live_action_english
live_action_pv
live_action_other
live_action_raw
picture
graphic
photo
software
apps
games
art
art_anime
art_doujin
art_games
art_manga
art_pictures
real_life
real_life_pictures
real_life_videos"
filter="none"
category="all"
page=1
instance="https://nyaa.si"
exitsig=false
rss_mode=false

dmenu="${DMENU_COMMAND:-dmenu}"

while ! ${exitsig}; do
exitsig=true
while true; do
	selection=$(printf "search\nchoose filter\nchoose category\nchoose instance\nchoose mode\n" | $dmenu "$@" -p "Nyaash: ")
	case $selection in
		"search")
			break
			;;
		"choose filter")
			user_filter="$(printf "%s\n" "$filter_list" | $dmenu "$@" -p "(Nyaash) Pick filter")"
			if [ -n "$user_filter" ]; then
				filter="$user_filter"
			fi
			;;
		"choose category")
			user_category="$(printf "%s\n" "$category_list" | $dmenu "$@" -p "(Nyaash) Pick category")"
			if [ -n "$user_category" ]; then
				category="$user_category"
			fi
			;;
		"choose instance")
			case $(printf "https://nyaa.si\nhttps://sukebei.nyaa.si\ncustom" | $dmenu "$@" -p "(Nyaash) instance: ") in
				"https://nyaa.si")
					instance="https://nyaa.si"
					;;
				"https://sukebei.nyaa.si")
					instance="https://sukebei.nyaa.si"
					;;
				"custom")
					custom_instance="$(echo "" | $dmenu "$@" -p "(Nyaash) Custom Instance: ")"
					if [ -n "$custom_instance" ]; then
						instance="$custom_instance"
					fi
					;;
				*)
					;;
			esac
			;;
		"choose mode")
			case $(printf "normal\nrss" | $dmenu "$@" -p "(Nyaash) mode:") in
				"normal")
					rss_mode=false
					;;
				"rss")
					rss_mode=true
					;;
				*)
					;;
			esac
			;;

		*)
			if [ -z "$selection" ]; then
				exit 0
			fi
			break
			;;
	esac
done

if [ "$selection" = "search" ]; then
	query=$(echo "" | $dmenu "$@" -p "(Nyaash) Search: ")
else
	query=$selection
fi

new_search=true
while true; do 
	if $new_search; then
		if $rss_mode; then
			output=$(nyaash "$query" -t "$category" -f "$filter" -i "$instance" -p $page -r -l)
			lines=1
			namesizes="$output"
		else
			output=$(nyaash "$query" -t "$category" -f "$filter" -i "$instance" -p $page)
			names=$(echo "$output" | awk '!((NR+4)%5)' | sed 's/^[0-9]* //')
			magnets=$(echo "$output" | awk '!((NR+3)%5)')
			sizes=$(echo "$output" | awk '!((NR+2)%5)' | grep -Eo "[0-9]+(\.[0-9]+)? (.i)?B")
			lines=$(echo "$names" | wc -l)
			unset namesizes
			while [ -n "$names" ]; do
				name=$(printf "%s" "$names" | head -n 1);
				size=$(printf "%s" "$sizes" | head -n 1);
				if [ -z "$namesizes" ]; then
					namesizes="$size $name"
				else
					namesizes=$(printf "%s\n%s %s" "$namesizes" "$size" "$name")
				fi
				names=$(echo "$names" | tail -n +2)
				sizes=$(echo "$sizes" | tail -n +2)
			done
		fi
	else
		new_search=true
	fi
	if [ "$lines" -gt 0 ]; then
		if [ $page -eq 1 ] && [ "$lines" -eq 75 ]; then
			selected="$(printf "main menu\nnext page\n%s\nnext page\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
		elif [ "$page" -gt 1 ] && [ "$lines" -eq 75 ]; then
			selected="$(printf "main menu\nnext page\nprevious page\n%s\nprevois page\nnext page\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
		elif [ $page -eq 1 ]; then # It's the first page, but has less than 75 items -> also the last page
			selected="$(printf "main menu\n%s\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
		else # It's not the first page, but it has less than 75 items -> it's the last page
			selected="$(printf "main menu\nprevious page\n%s\nprevous page\n main menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
		fi
	elif [ $page -gt 1 ]; then
		selected="$(printf "main menu\nprevious page\n" | $dmenu "$@" -p "(Nyaash) Download: ")"
	else
		selected="$(printf "no results\nmain menu\n" | $dmenu "$@" -p "(Nyaash) Download: ")"
	fi
	case "$selected" in
		"next page")
			page=$(( page + 1))
			;;
		"previous page")
			page=$(( page - 1))
			;;
		"main menu")
			exitsig=false
			break
			;;
		"")
			break
			;;
		*)
			if $rss_mode; then
				if [ "$(printf "copy\nopen" | $dmenu "$@" -p "Nyaash:")" = "copy" ]; then
					printf "%s" "$selected" | cb copy
				else
					xdg-open "$selected"
				fi
			else
				echo "$selected" | while read -r name; do
					selectedline=$(echo "$namesizes" | grep -nF "$name" | grep -Eo '^[0-9]+') 
					xdg-open "$(printf "%s" "$magnets" | awk "NR==$selectedline")" &
				done
			fi
			choice=$(printf "yes\nno\n" | $dmenu "$@" -p "(Nyaash) Select another torrent?" )
			if ! echo "$choice" | grep -Eq '[Yy].*'; then
				break
			else
				new_search=false
			fi
			;;
	esac
done
done
